package com.sgb.kjwl.viewmodel.transport;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.CarrierDetailsEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.TransportRepertory;
import com.sgb.kjwl.view.ui.activity.transport.MoreCarriageInfoActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Time:2021/1/11
 * Author:黄淋
 * Description:
 */
public class MoreCarriageInfoModel extends BaseViewModel {
    private MoreCarriageInfoActivity mActivity;
    private TransportRepertory repertory;
    private int pageNum = 1;

    public MoreCarriageInfoModel(MoreCarriageInfoActivity activity, TransportRepertory repertory) {
        this.mActivity = activity;
        this.repertory = repertory;
    }

    public void getCarriageListData(boolean isLodMore, Map map) {
        pageNum = isLodMore ? pageNum + 1 : 1;
        //limit
        map.put("limit", 20);
        //page
        map.put("page", pageNum);

        DialogHelper.showProgressDialog(mActivity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().getCarriageList(map.get("userNo") + "", map.get("driverAuthNo") + ""
                , map.get("page") + "", map.get("limit") + "").enqueue(new Callback<BaseEntity<ListResponse<List<CarrierDetailsEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<CarrierDetailsEntity>>>> call, Response<BaseEntity<ListResponse<List<CarrierDetailsEntity>>>> response) {
                DialogHelper.dismissProgressDialog();

                if (response.body() == null || response.body().getState() == null || response.body().getData() == null)
                    return;

                if (response.body().getState().equals("ok")) {
                    mActivity.showList(response.body().getData().getList(), isLodMore);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<CarrierDetailsEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
